package C036;

/**
 * ClassName: C03
 * Package: C036
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/20 17:35
 * @Version 1.0
 * @Since 1.0
 */
public class C03 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode2.left = treeNode4;
        treeNode3.right = treeNode5;
        System.out.println(widthOfBinaryTree(treeNode1));
    }

    // 不提交这个类
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    static int MAXLEN = 3001;
    static TreeNode[] queue = new TreeNode[MAXLEN];
    static int[] eleIndex = new int[MAXLEN];
    static int r, l;
    public static int widthOfBinaryTree(TreeNode root) {
        if(root == null){
            return 0;
        }

        l = r = 0;
        int ans = 0;
        queue[l] = root;
        eleIndex[l] = 1;
        r ++;
        int size;
        while(l < r) {
            size = r - l;
            ans = Math.max(ans, eleIndex[r- 1] - eleIndex[l] + 1);
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue[l];
                int index = eleIndex[l ++];
                if(cur.left != null){
                    queue[r] = cur.left;
                    eleIndex[r++] = index * 2;
                }
                if(cur.right != null){
                    queue[r] = cur.right;
                    eleIndex[r++] = index * 2 + 1;
                }
            }
        }

        return ans;
    }
}
